<?php
namespace BoxMall\Logic;
class CardLogic extends \LogicController
{

    /*******验证优惠券是否可以用***/
    public function check_card($num, $need_password = 0, $password)
    {
        $data = BoxModel("SystemCard")->load($num);
        if (!$data) {
            return $this->return_array("3", "卡券号码不正确");
        }

        if ($need_password && $data['password'] != $password) {
            return $this->return_array("3", "卡券密码不正确");
        }
        $date = date("Y-m-d");
        if ($data['invalid_time'] < $date) {
            return $this->return_array("3", "卡券已经过期");
        }
        if ($data['state'] == 1) {
            return $this->return_array("4", "卡券未激活");
        }
        if ($data['state'] == 3) {
            return $this->return_array("5", "卡券未被冻结");
        }
        if ($data['state'] == 4) {
            return $this->return_array("6", "卡券已作废");
        }
        if ($data['state'] == 5) {
            return $this->return_array("7", "卡券已使用");
        }
        if ($data['user_id']) {
            return $this->return_array("8", "卡券已被用户绑定");
        }
        return $this->return_array(0, $data);
    }

    //使用卡券
    public function use_card($user_id, $card_num)
    {
        $r = BoxModel("system_card")->where("num='$card_num'")->save(
            array('state' => 5, 'user_id' => $user_id, 'recharge_time' => date("Y-m-d H:i:s"))
        );
        return $r;
    }

}